package servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.EmailingSystem;
import model.PollUserImpl;
import bean.PollUser;
public class CreateUserServlet extends HttpServlet {


	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2566142539284129110L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		int accessibility = Integer.parseInt(request.getParameter("accessibility"));
		String email = request.getParameter("email");
		
		PollUser user = new PollUser();
		user.setUsername(username);
		user.setPassword(password);
		user.setAccessibility(accessibility);
		user.setEmail(email);

		PollUserImpl pui = new PollUserImpl();
		pui.addUser(user);
		EmailingSystem.sendAccountEmail(user);
		
		boolean bool = pui.checkuser(user);
		
		String forward;
		if (bool) {
			forward = "../success.jsp";
		} else {
			forward = "../failure.jsp";
		}
		RequestDispatcher rd=request.getRequestDispatcher(forward);
		rd.forward(request, response);
	}
}
